package com.anda.driver.locator.activity;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.anda.driver.locator.R;

public class VideoRecordActivity extends Activity implements Callback{
	private SurfaceView videoPreview;
	private Button buttonCapture;
	private SurfaceHolder holder;
	private boolean mVideoOn = false;
	private MediaRecorder mMediaRecorder = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video);
        videoPreview = (SurfaceView)findViewById(R.id.videoPreview);
        holder = videoPreview.getHolder();
        holder.addCallback(this);
        
        buttonCapture = (Button)findViewById(R.id.buttonCapture);
        buttonCapture.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				if(!mVideoOn){
					mVideoOn = true;
					startVideoCapture(1);
				} else {
					mVideoOn = false;
				}
			}
        });
    }
    
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		
	}
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		this.holder = holder;
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		
	}

    private void startVideoCapture(int i)
    {
        if(i == 1)
        {
//            HashMap hashmap = new HashMap();
//            hashmap.put("video_quality", settings.getString("video_quality", Voyager.videoQualityPrefDef));
//            hashmap.put("video_res", settings.getString("video_res", Voyager.videoResPrefDef));
//            hashmap.put("video_bitrate", settings.getString("video_bitrate", Voyager.videoBitratePrefDef));
//            hashmap.put("video_codec", settings.getString("video_codec", Voyager.videoCodecPrefDef));
//            hashmap.put("video_stability", settings.getString("video_stability", Voyager.videoStabilityPrefDef));
//            hashmap.put("video_length", settings.getString("video_length", Voyager.videoLengthPrefDef));
//            hashmap.put("video_sound2", (new StringBuilder()).append(settings.getString("video_sound2", Voyager.videoSoundPrefDef)).toString());
//            hashmap.put("video_file_rescue", settings.getString("video_file_rescue", Voyager.videoFileRescuePrefDef));
//            hashmap.put("video_file_upload", settings.getString("video_file_upload", Voyager.videoFileUploadPrefDef));
//            hashmap.put("video_file_size", settings.getString("video_file_size", Voyager.videoFileSizePrefDef));
//            FlurryAgent.onEvent("startVideoCapture", hashmap);
        }

        mMediaRecorder.start();
    }

  
    private boolean initializeVideo()
    {
        if(mMediaRecorder == null){
        	mMediaRecorder = new MediaRecorder();
        }
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        
        mMediaRecorder.setOutputFile(getOutputMediaFile().toString());
        mMediaRecorder.setPreviewDisplay(holder.getSurface());
        mMediaRecorder.setMaxDuration(1000 * 60);
        try {
			mMediaRecorder.prepare();
		} catch (IllegalStateException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
        return true;
       
    }
    
    /** Create a File for saving an image or video */
	private static File getOutputMediaFile(){
	    // To be safe, you should check that the SDCard is mounted
	    // using Environment.getExternalStorageState() before doing this.
	    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
	              Environment.DIRECTORY_PICTURES), "anda");
	    // This location works best if you want the created images to be shared
	    // between applications and persist after your app has been uninstalled.

	    // Create the storage directory if it does not exist
	    if (! mediaStorageDir.exists()){
	        if (! mediaStorageDir.mkdirs()){
	            Log.d("MyCameraApp", "failed to create directory");
	            return null;
	        }
	    }

	    // Create a media file name
	    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
	    File mediaFile = new File(mediaStorageDir.getPath() + File.separator +
	        "VID_"+ timeStamp + ".mp4");

	    return mediaFile;
	}
	
}